<template>
  <div class="main">
    <!-- 顶部功能 -->
    <el-form ref="addDtoRef" :inline="true" class="bg-bg_color w-[99/100] pl-4 pt-4">
      <el-form-item label="模块名称">
        <el-select style="width: 160px" placeholder="请选择" filterable clearable
          @change="GetModulChildrens(SubmitFrom.moduleId)" value-key="displayName" v-model="SubmitFrom.moduleId">
          <el-option v-for="item in ListFrom.ModelList" :key="item.id" :label="item.displayName" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="菜单名称">
        <el-select style="width: 160px" placeholder="请选择" filterable clearable value-key="displayName"
          v-model="SubmitFrom.menuId">
          <el-option v-for="item in ListFrom.ChildList" :key="item.id" :label="item.displayName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="题型">
        <el-select style="width: 100px" placeholder="请选择" filterable clearable v-model="SubmitFrom.questionType">
          <el-option v-for="item in ListFrom.QuestionType" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="题目">
        <el-input type="text" style="width: 160px" v-model="SubmitFrom.title" />
      </el-form-item>
      <el-form-item>
        <el-button :icon="useRenderIcon(Search)" type="primary" @click="SearchForm()">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <PureTableBar title="机构列表" class="h100-60">
      <template #buttons>
        <p class="text-red-500 absolute left-[120px] leading-7 text-sm">注：双击查看详情</p>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="handleUpdate(undefined)">
          添加题目
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" :loading="loading" :size="size"
          :data="dataList" :columns="columns" :checkList="checkList" :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }" @selection-change="handleSelectionChange" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" @cell-dblclick="handleUpdate">
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
              @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button class="reset-margin" link type="danger" :size="size" @click="handleDelete(row)"
              :icon="useRenderIcon(Delete)">
              删除
            </el-button>
            <!-- <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)">
                  删除
                </el-button>
              </template>
            </el-popconfirm> -->
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <el-dialog top="10vh" @close="HideVisible(subcheck)" v-model="updatePanelVisible" :title="VisibleTitle + '题目'"
      width="55%" center :close-on-click-modal="false">
      <el-scrollbar height="600px">
        <el-form :model="updateFrom" :rules="rules" ref="subcheck" label-width="120px">
          <el-form-item label="模块名称" prop="moduleId">
            <el-select class="w-220" placeholder="请选择" filterable clearable :disabled="isdisabled"
              @change="GetModulChildrenss(updateFrom.moduleId)" value-key="displayName" v-model="updateFrom.moduleId">
              <el-option v-for="item in ListFroms.ModelList" :key="item.id" :label="item.displayName" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="菜单名称" prop="menuId">
            <el-select class="w-220" placeholder="请选择" filterable clearable :disabled="isdisabled" value-key="displayName"
              v-model="updateFrom.menuId">
              <el-option v-for="item in ListFroms.ChildList" :key="item.id" :label="item.displayName" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="题型" prop="questionType">
            <el-select class="w-220" placeholder="请选择" filterable clearable :disabled="isdisabled"
              v-model="updateFrom.questionType">
              <el-option v-for="item in ListFroms.QuestionType" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="题目" prop="title" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.title" :height="200" />
          </el-form-item>
          <el-form-item label="选项A" prop="answerA" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.answerA" :height="200" />
          </el-form-item>
          <el-form-item label="选项B" prop="answerB" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.answerB" :height="200" />
          </el-form-item>
          <el-form-item label="选项C" prop="answerC" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.answerC" :height="200" />
          </el-form-item>
          <el-form-item label="选项D" prop="answerD" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.answerD" :height="200" />
          </el-form-item>
          <el-form-item label="选项E" prop="answerE" v-if="updatePanelVisible">
            <wang-editor v-model="updateFrom.answerE" :height="200" />
          </el-form-item>
          <el-form-item label="答案" prop="trueAnswer">
            <el-input type="text" @keyup="checkContent" v-model="updateFrom.trueAnswer" placeholder="请填写答案，多选如：ABCD"
              oninput="value=value.replace(/[^A-E]/g,'');" maxlength="6" show-word-limit class="!w-[700px]" />
          </el-form-item>
          <el-form-item label="解析" prop="remark">
            <el-input v-model="updateFrom.remark" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"
              placeholder="Please input" class="!w-[700px]" />
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="SaveTopic(subcheck)">保存</el-button>
          <el-button @click="HideVisible(subcheck)">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { PureTableBar } from "@/components/RePureTableBar";
import WangEditor from "@/components/wangEditor/index.vue";
import { ref } from "vue";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import AddFill from "@iconify-icons/ri/add-circle-line";
import { useIndex } from "./hook";
// 校验只能输入英文
function checkContent(event) {
  updateFrom.trueAnswer = updateFrom.trueAnswer.replace(/[^A-Z]/g, "");
}
const subcheck = ref();
const {
  rules,
  columns,
  loading,
  ListFrom,
  dataList,
  ListFroms,
  isdisabled,
  updateFrom,
  SubmitFrom,
  pagination,
  VisibleTitle,
  updatePanelVisible,
  SaveTopic,
  SearchForm,
  HideVisible,
  handleUpdate,
  handleDelete,
  handleSizeChange,
  GetModulChildrens,
  GetModulChildrenss,
  handleCurrentChange,
  handleSelectionChange
} = useIndex();
</script>
